import { http } from '@/utils/request'
import { useCaseStore } from '@/stores/caseStore'

// 案件相关API
export const caseAPI = {
  // 获取案件列表
  async getCaseList(params = {}) {
    try {
      const response = await http.get('/cases', params)
      // 更新状态库中的案件列表
      const caseStore = useCaseStore()
      caseStore.caseList = {
        items: response.items || [],
        page: response.page || 1,
        pageSize: response.pageSize || 10,
        total: response.total || 0
      }
      return response
    } catch (error) {
      console.error('获取案件列表失败:', error)
      throw error
    }
  },
  
  // 获取案件详情
  async getCaseDetail(id) {
    try {
      const response = await http.get(`/cases/${id}`)
      // 更新当前案件
      const caseStore = useCaseStore()
      caseStore.currentCase = response
      return response
    } catch (error) {
      console.error('获取案件详情失败:', error)
      throw error
    }
  },
  
  // 创建案件
  async createCase(data) {
    try {
      const response = await http.post('/cases', data)
      // 创建成功后添加到案件列表
      const caseStore = useCaseStore()
      caseStore.caseList.items.unshift(response)
      caseStore.caseList.total += 1
      return response
    } catch (error) {
      console.error('创建案件失败:', error)
      throw error
    }
  },
  
  // 更新案件
  async updateCase(id, data) {
    try {
      const response = await http.put(`/cases/${id}`, data)
      // 更新状态库中的案件
      const caseStore = useCaseStore()
      if (caseStore.currentCase.id === id) {
        caseStore.currentCase = { ...caseStore.currentCase, ...response }
      }
      // 更新列表中的案件
      const index = caseStore.caseList.items.findIndex(c => c.id === id)
      if (index !== -1) {
        caseStore.caseList.items[index] = { ...caseStore.caseList.items[index], ...response }
      }
      return response
    } catch (error) {
      console.error('更新案件失败:', error)
      throw error
    }
  },
  
  // 删除案件
  async deleteCase(id) {
    try {
      await http.delete(`/cases/${id}`)
      // 从状态库中删除案件
      const caseStore = useCaseStore()
      caseStore.caseList.items = caseStore.caseList.items.filter(c => c.id !== id)
      caseStore.caseList.total -= 1
      // 如果删除的是当前案件，清空当前案件
      if (caseStore.currentCase.id === id) {
        caseStore.currentCase = {
          id: '',
          type: '',
          description: '',
          materials: [],
          status: 'draft'
        }
      }
    } catch (error) {
      console.error('删除案件失败:', error)
      throw error
    }
  },
  
  // 上传案件材料
  async uploadMaterial(caseId, formData) {
    try {
      const response = await http.post(`/cases/${caseId}/materials`, formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      // 更新当前案件的材料列表
      const caseStore = useCaseStore()
      if (caseStore.currentCase.id === caseId) {
        caseStore.currentCase.materials.push(response)
      }
      return response
    } catch (error) {
      console.error('上传材料失败:', error)
      throw error
    }
  },
  
  // 获取案件材料列表
  async getCaseMaterials(caseId) {
    try {
      const response = await http.get(`/cases/${caseId}/materials`)
      // 更新当前案件的材料列表
      const caseStore = useCaseStore()
      if (caseStore.currentCase.id === caseId) {
        caseStore.currentCase.materials = response
      }
      return response
    } catch (error) {
      console.error('获取案件材料失败:', error)
      throw error
    }
  },
  
  // 删除案件材料
  async deleteMaterial(caseId, materialId) {
    try {
      await http.delete(`/cases/${caseId}/materials/${materialId}`)
      // 从当前案件中删除材料
      const caseStore = useCaseStore()
      if (caseStore.currentCase.id === caseId) {
        caseStore.currentCase.materials = caseStore.currentCase.materials.filter(
          m => m.id !== materialId
        )
      }
    } catch (error) {
      console.error('删除材料失败:', error)
      throw error
    }
  },
  
  // 更新案件状态
  async updateCaseStatus(id, status) {
    try {
      const response = await http.patch(`/cases/${id}/status`, { status })
      // 更新状态库中的案件状态
      const caseStore = useCaseStore()
      if (caseStore.currentCase.id === id) {
        caseStore.currentCase.status = status
      }
      // 更新列表中的案件状态
      const index = caseStore.caseList.items.findIndex(c => c.id === id)
      if (index !== -1) {
        caseStore.caseList.items[index].status = status
      }
      return response
    } catch (error) {
      console.error('更新案件状态失败:', error)
      throw error
    }
  },
  
  // 添加案件进度记录
  addCaseProgress: (id, data) => http.post(`/cases/${id}/progress`, data),
  
  // 获取案件进度记录
  getCaseProgress: (id) => http.get(`/cases/${id}/progress`)
} 